package com.xyht.sca_s.student_manage_system.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsWnsChargeClassificationRelation;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsWnsChargeClassificationRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoods;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoodsClassification;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoodsStock;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsClassificationAddReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsClassificationDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.req.SmsGoodsClassificationUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.resp.SmsGoodsClassificationResp;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.resp.SmsGoodsResp;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsClassificationMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsStockMapper;
import com.xyht.sca_s.student_manage_system.modules.goods.service.SmsGoodsClassificationService;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsChargeCategoryDetail;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsChargeCategoryDetailMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.chargeTypeConstant.CHARGE_TYPE_GOODS;

/**
 * <p>
 * 物品分类表(--迎新) 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-17
 */
@Service
public class SmsGoodsClassificationServiceImpl extends ServiceImpl<SmsGoodsClassificationMapper, SmsGoodsClassification> implements SmsGoodsClassificationService {

    @Resource
    private SmsGoodsClassificationMapper goodsClassificationMapper;
    @Resource
    private SmsGoodsMapper goodsMapper;
    @Resource
    private SmsGoodsStockMapper goodsStockMapper;
    @Resource
    private SmsWnsChargeClassificationRelationMapper chargeClassificationRelationMapper;
    @Resource
    private SmsWnsChargeCategoryDetailMapper chargeCategoryDetailMapper;

    @Override
    @Transactional
    public ResponseResult addGoodsClassification(String userId, SmsGoodsClassificationAddReq addReq) {
        if (isNullOrEmpty(addReq.getClassificationName(),addReq.getGoodsId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验物品是否存在
        SmsGoods smsGoods = goodsMapper.selectById(addReq.getGoodsId());
        if (isNullOrEmpty(smsGoods)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NOT_EXIST);
        }
        //校验物品分类名是否重复
        List<SmsGoodsClassification> classificationList = goodsClassificationMapper.selectList(new LambdaQueryWrapper<SmsGoodsClassification>()
                .eq(SmsGoodsClassification::getClassificationName, addReq.getClassificationName()));
        if (!classificationList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.GOODS_CLASSIFICATION_NAME_EXIST);
        }
        SmsGoodsClassification smsGoodsClassification = new SmsGoodsClassification();
        smsGoodsClassification.setGoodsId(addReq.getGoodsId());
        smsGoodsClassification.setClassificationName(addReq.getClassificationName());
        smsGoodsClassification.setUserId(userId);
        goodsClassificationMapper.insert(smsGoodsClassification);

        String goodsClassificationId = smsGoodsClassification.getId();
        if (isNullOrEmpty(goodsClassificationId)) {
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        //添加物品分类库存默认0
        SmsGoodsStock smsGoodsStock = new SmsGoodsStock();
        smsGoodsStock.setGoodsId(addReq.getGoodsId());
        smsGoodsStock.setClassificationId(goodsClassificationId);
        smsGoodsStock.setStockQuantity(0);
        goodsStockMapper.insert(smsGoodsStock);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedGoodsClassification(SmsGoodsClassificationDeletedReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsGoodsClassification smsGoodsClassification = goodsClassificationMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsGoodsClassification)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_CLASSIFICATION_NOT_EXIST);
        }
        //删除分类之前查看分类是否绑定收费类别
        Integer count = chargeClassificationRelationMapper.selectCount(new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                .eq(SmsWnsChargeClassificationRelation::getClassificationId, deletedReq.getId())
                .eq(SmsWnsChargeClassificationRelation::getType, CHARGE_TYPE_GOODS));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.CHARGE_BIND_EXIST);
        }
        goodsClassificationMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateGoodsClassification(SmsGoodsClassificationUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getGoodsId(),updateReq.getClassificationName(),updateReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验物品分类是否存在
        SmsGoodsClassification smsGoodsClassification = goodsClassificationMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsGoodsClassification)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_CLASSIFICATION_NOT_EXIST);
        }

        //校验物品是否存在
        SmsGoods smsGoods = goodsMapper.selectById(updateReq.getGoodsId());
        if (isNullOrEmpty(smsGoods)) {
            return CommonResult.failed(CommonCodeEnum.GOODS_NOT_EXIST);
        }
        //校验物品分类名是否重复
        List<SmsGoodsClassification> classificationList = goodsClassificationMapper.selectList(new LambdaQueryWrapper<SmsGoodsClassification>()
                .eq(SmsGoodsClassification::getClassificationName, updateReq.getClassificationName())
                .ne(SmsGoodsClassification::getId, updateReq.getId()));
        if (!classificationList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.GOODS_CLASSIFICATION_NAME_EXIST);
        }
        BeanUtils.copyProperties(updateReq,smsGoodsClassification);
        goodsClassificationMapper.updateById(smsGoodsClassification);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getGoodsClassificationList(Integer pageNum, Integer pageSize, String name, String classificationId, Integer isOn) {
        Page<SmsGoodsClassification> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsGoodsClassification> classificationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(name)) {
            classificationLambdaQueryWrapper.like(SmsGoodsClassification::getClassificationName,name);
        }
        if (!isNullOrEmpty(classificationId)) {
            classificationLambdaQueryWrapper.eq(SmsGoodsClassification::getId,classificationId);
        }
        if (!isNullOrEmpty(isOn)) {
            List<SmsGoods> goodsList = goodsMapper.selectList(new LambdaQueryWrapper<SmsGoods>()
                    .eq(SmsGoods::getIsOn, isOn));
            if (!goodsList.isEmpty()) {
                List<String> goodsIdList = goodsList.stream().map(SmsGoods::getId).collect(Collectors.toList());
                classificationLambdaQueryWrapper.in(SmsGoodsClassification::getGoodsId,goodsIdList);
            }else {
                classificationLambdaQueryWrapper.in(SmsGoodsClassification::getGoodsId,"");
            }
        }
        goodsClassificationMapper.selectPage(page,classificationLambdaQueryWrapper);
        List<SmsGoodsClassificationResp> goodsClassificationList = page.getRecords()
                .stream()
                .map(smsGoodsClassification -> {
                    SmsGoodsClassificationResp smsGoodsClassificationResp = new SmsGoodsClassificationResp();
                    SmsGoodsResp smsGoodsResp = new SmsGoodsResp();
                   //物品信息
                    SmsGoods smsGoods = goodsMapper.selectById(smsGoodsClassification.getGoodsId());
                    if (!isNullOrEmpty(smsGoods)) {
                        BeanUtils.copyProperties(smsGoods,smsGoodsResp);
                    }
                    //分类库存
                    List<SmsGoodsStock> smsGoodsStocks = goodsStockMapper.selectList(new LambdaQueryWrapper<SmsGoodsStock>()
                            .eq(SmsGoodsStock::getGoodsId, smsGoodsClassification.getGoodsId())
                            .eq(SmsGoodsStock::getClassificationId, smsGoodsClassification.getId()));
                    int stockQuantity = 0;
                    if (!smsGoodsStocks.isEmpty()) {
                        stockQuantity = smsGoodsStocks.get(0).getStockQuantity();
                        smsGoodsClassificationResp.setStockId(smsGoodsStocks.get(0).getId());
                    }
                    // //收费标准
                    List<SmsWnsChargeClassificationRelation> relationList = chargeClassificationRelationMapper.selectList(new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                            .eq(SmsWnsChargeClassificationRelation::getId, smsGoodsClassification.getId())
                            .eq(SmsWnsChargeClassificationRelation::getType, CHARGE_TYPE_GOODS));
                    if (!relationList.isEmpty()) {
                        String chargeDetailId = relationList.get(0).getChargeDetailId();
                        SmsWnsChargeCategoryDetail smsWnsChargeCategoryDetail = chargeCategoryDetailMapper.selectById(chargeDetailId);
                        if (!isNullOrEmpty(smsWnsChargeCategoryDetail)) {
                            smsGoodsClassificationResp.setCharge(smsWnsChargeCategoryDetail.getDefaultPrice());
                        }

                    }
                    smsGoodsClassificationResp.setGoodsInfo(smsGoodsResp);
                    smsGoodsClassificationResp.setStockQuantity(stockQuantity);
                    smsGoodsClassificationResp.setId(smsGoodsClassification.getId());
                    smsGoodsClassificationResp.setClassificationName(smsGoodsClassification.getClassificationName());
                    return smsGoodsClassificationResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(goodsClassificationList,total);
    }
}
